<?php
header("Content-type:text/html;charset=utf-8");
require '../cofd/common.php';
$sessionLifetime = 7200; //登录令牌有效期，默认2H
    session_set_cookie_params([
        'lifetime' => $sessionLifetime,
        'path' => '/',
        'domain' => $_SERVER['HTTP_HOST'],
        'secure' => $_SERVER['HTTPS'] ?? false,
        'httponly' => false,
    ]);
function getlogin($user, $pass){
global $conn;
global $sessionLifetime;
$stmt = $conn->prepare("SELECT * FROM exam_student WHERE username = ? AND password = ?");  
$stmt->bind_param("ss", $user, $pass); 
$stmt->execute();  
$result = $stmt->get_result(); 
$row = $result->fetch_assoc(); 
if ($result->num_rows > 0) {    
    $userId = htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8');
    $userName = htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8');
    setcookie('exam_userid_s',$userId, 0, '/');
    setcookie('exam_username_s',$userName, 0, '/');
    echo "<script>sessionStorage.setItem('exam_userid_s', '" . $userId . "');</script>";
    echo "<script>sessionStorage.setItem('exam_username_s', '" . $userName . "');</script>";
    session_start();
    $_SESSION['authorized_s'] = true;
    $_SESSION['expires_at_s'] = time() + $sessionLifetime;
    if($row['smrz'] !== '' && $row['smrz'] !== null){
        $_SESSION['exam_smrz_s'] = true;
    }else{
        $_SESSION['exam_smrz_s'] = false;
    }
    session_write_close();
    echo "<script>alert('账号密码正确！登录成功');location='./index.php';</script>";  
} else {  
    echo "<script>alert('管理员账号或密码错误!');location='./?juslr';</script>";  
}   
$stmt->close();  
$conn->close(); 
}
?>